#!/bin/bash

# Gateway路由测试脚本
# 用于验证crossborder模块的路由配置是否正确

GATEWAY_URL="http://localhost:48080"

echo "=== Gateway路由配置测试 ==="
echo "Gateway地址: $GATEWAY_URL"
echo ""

# 测试函数
test_route() {
    local url=$1
    local expected_service=$2
    local description=$3
    
    echo "测试: $description"
    echo "URL: $url"
    echo "期望路由到: $expected_service"
    
    # 发送请求并获取响应状态码
    status_code=$(curl -s -o /dev/null -w "%{http_code}" "$url")
    
    if [ "$status_code" = "200" ]; then
        echo "✅ 状态码: $status_code (成功)"
    elif [ "$status_code" = "401" ] || [ "$status_code" = "403" ]; then
        echo "⚠️  状态码: $status_code (需要认证，但路由正常)"
    elif [ "$status_code" = "404" ]; then
        echo "❌ 状态码: $status_code (路由失败或服务未启动)"
    else
        echo "⚠️  状态码: $status_code (其他状态)"
    fi
    echo "---"
}

echo "开始测试crossborder模块路由..."
echo ""

# 测试订单相关接口 -> crossborder-order-server
test_route "$GATEWAY_URL/admin-api/crossborder/order-item/page?pageNo=1&pageSize=10" \
           "crossborder-order-server" \
           "订单明细分页接口"

test_route "$GATEWAY_URL/admin-api/crossborder/order/list" \
           "crossborder-order-server" \
           "订单列表接口"

test_route "$GATEWAY_URL/admin-api/crossborder/order-shipment/list" \
           "crossborder-order-server" \
           "发货单列表接口"

# 测试商品相关接口 -> crossborder-product-server
test_route "$GATEWAY_URL/admin-api/crossborder/product/list" \
           "crossborder-product-server" \
           "商品列表接口"

test_route "$GATEWAY_URL/admin-api/crossborder/category/list" \
           "crossborder-product-server" \
           "商品分类接口"

# 测试库存相关接口 -> crossborder-inventory-server
test_route "$GATEWAY_URL/admin-api/crossborder/inventory/list" \
           "crossborder-inventory-server" \
           "库存列表接口"

test_route "$GATEWAY_URL/admin-api/crossborder/warehouse/list" \
           "crossborder-inventory-server" \
           "仓库列表接口"

# 测试物流相关接口 -> crossborder-logistics-server
test_route "$GATEWAY_URL/admin-api/crossborder/logistics/list" \
           "crossborder-logistics-server" \
           "物流列表接口"

# 测试财务相关接口 -> crossborder-finance-server
test_route "$GATEWAY_URL/admin-api/crossborder/finance/list" \
           "crossborder-finance-server" \
           "财务列表接口"

# 测试平台对接接口 -> crossborder-platform-server (兜底路由)
test_route "$GATEWAY_URL/admin-api/crossborder/platform/health" \
           "crossborder-platform-server" \
           "平台健康检查接口"

test_route "$GATEWAY_URL/admin-api/crossborder/webhook/test" \
           "crossborder-platform-server" \
           "Webhook测试接口"

echo ""
echo "=== 测试完成 ==="
echo ""
echo "说明："
echo "✅ 200状态码表示路由成功且接口正常"
echo "⚠️  401/403状态码表示路由成功但需要认证"
echo "❌ 404状态码表示路由失败或目标服务未启动"
echo ""
echo "如果看到404错误，请检查："
echo "1. 对应的微服务是否已启动"
echo "2. 服务是否已注册到Nacos"
echo "3. Gateway配置是否正确"
echo "4. 接口路径是否存在"
